#!/usr/bin/env python
# coding=utf-8
# __author__ = 'Yunchao Ling'

import pycurl
import StringIO
import datetime

NOW = datetime.datetime.now()
NOW_DATE = NOW.strftime("%Y%m%d")


def Verify_Meta_XML():
    import commands

    cmd = "grep '</pmc-articleset>' pmcmeta_" + NOW_DATE + ".xml"
    result = commands.getoutput(cmd)
    if result.find("</pmc-articleset>") != -1:
        print "pmcmeta_" + NOW_DATE + ".xml is verified."
        return True
    elif result.find("No such file or directory") != -1:
        return False
    else:
        print "pmcmeta_" + NOW_DATE + ".xml is invalid. Start redownloading..."
        return False


def Get_PMC_Meta(ids):
    url = 'http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi'
    buf = StringIO.StringIO()
    c = pycurl.Curl()
    c.setopt(pycurl.POST, 1)
    c.setopt(c.URL, url)
    c.setopt(pycurl.HTTPPOST, [('db', 'pmc'), ('id', ids), ('retmode', 'xml')])
    c.setopt(c.WRITEFUNCTION, buf.write)
    c.perform()
    outfile = open("pmcmeta_" + NOW_DATE + ".xml", "w")
    outfile.write(buf.getvalue())
    outfile.close()
    buf.close()
    print "pmcmeta_" + NOW_DATE + " has been successfully downloaded."


def Get_IDs():
    ids = ""
    infile = open("pmcid_todo_" + NOW_DATE + ".txt", "r")
    for line in infile:
        line = line.rstrip()
        ids += line + ","
    infile.close()
    ids = ids[:-1]
    return ids


if __name__ == '__main__':
    print "Today is " + NOW_DATE + "."
    print "Loading PMCIDs ..."
    ids = Get_IDs()
    print "Downloading PMC Meta ..."
    while not Verify_Meta_XML():
        Get_PMC_Meta(ids)
